package com.rainmonth.common.widgets;

import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;

public class CustomViewPager extends ViewPager {
        private ArrayList<Integer> childCenterXAbs = new ArrayList<>();
        private SparseArray<Integer> childIndex = new SparseArray<>();

        public CustomViewPager(Context context) {
            super(context);
            init();
        }

        public CustomViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        private void init() {
            setClipToPadding(false);
            setOverScrollMode(OVER_SCROLL_NEVER);
        }

        @Override
        protected int getChildDrawingOrder(int childCount, int n) {
            if (n == 0 || childIndex.size() != childCount) {
                childCenterXAbs.clear();
                childIndex.clear();
                int viewCenterX = getViewCenterX(this);
                for (int i = 0; i < childCount; ++i) {
                    int indexAbs = Math.abs(viewCenterX - getViewCenterX(getChildAt(i)));
                    //两个距离相同，后来的那个做自增，从而保持abs不同
                    if (childIndex.get(indexAbs) != null) {
                        ++indexAbs;
                    }
                    childCenterXAbs.add(indexAbs);
                    childIndex.append(indexAbs, i);
                }
                Collections.sort(childCenterXAbs);//1,0,2  0,1,2
            }
            //那个item距离中心点远一些，就先draw它。（最近的就是中间放大的item,最后draw）
            return childIndex.get(childCenterXAbs.get(childCount - 1 - n));
        }

        private int getViewCenterX(View view) {
            int[] array = new int[2];
            view.getLocationOnScreen(array);
            return array[0] + view.getWidth() / 2;
        }
    }